package com.woniuxy.school.dietary.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.school.dietary.food.entity.FoodInfo;
import com.woniuxy.school.dietary.food.service.FoodInfoService;
import com.woniuxy.school.dietary.store.entity.ReceiveTimeConfig;
import com.woniuxy.school.dietary.store.entity.StoreInfo;
import com.woniuxy.school.dietary.store.mapper.ReceiveTimeConfigMapper;
import com.woniuxy.school.dietary.store.mapper.StoreInfoMapper;
import com.woniuxy.school.dietary.store.model.dto.FindStoreByIdDto;
import com.woniuxy.school.dietary.store.model.dto.FindStoreDto;
import com.woniuxy.school.dietary.store.model.from.AddStoreInfoForm;
import com.woniuxy.school.dietary.store.model.from.FindStoreFrom;
import com.woniuxy.school.dietary.store.model.from.StoreTimeForm;
import com.woniuxy.school.dietary.store.service.StoreInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年05月14日
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {
    @Resource
    private StoreInfoMapper storeInfoMapper;
    @Resource
    private ReceiveTimeConfigMapper  receiveTimeConfigMapper;
    @Resource
    private FoodInfoService foodInfoService;

    @Override
    public FindStoreDto findStore(Long storeId ) {
        QueryWrapper<StoreInfo> wrapper = new QueryWrapper<>();
        if (storeId!=null){
            wrapper.eq("id", storeId);
        }

        return BeanUtil.toBean(storeInfoMapper.selectOne(wrapper), FindStoreDto.class);
    }

    @Override
    public FindStoreByIdDto findStoreById(Long id) {
        FindStoreByIdDto dto  =storeInfoMapper.findStoreById(id);
        return dto;
    }

    @Override
    public void addStoreTime(StoreTimeForm storeTimeForm, String userId) {
        ReceiveTimeConfig receiveTimeConfig = new ReceiveTimeConfig();
        Snowflake snowflake = new Snowflake(1, 1);
        receiveTimeConfig.setId(snowflake.nextId());
        receiveTimeConfig.setStoreId(Long.parseLong(userId));
        receiveTimeConfig.setStartTime(storeTimeForm.getStartTime());
        receiveTimeConfig.setEndTime(storeTimeForm.getEndTime());
        receiveTimeConfigMapper.insert(receiveTimeConfig);
    }

    @Override
    public void updateStoreTime(StoreTimeForm storeTimeForm, String userId) {
        LambdaQueryWrapper<ReceiveTimeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReceiveTimeConfig::getStoreId,userId);
        ReceiveTimeConfig receiveTimeConfig = receiveTimeConfigMapper.selectOne(wrapper);
        receiveTimeConfig.setStartTime(storeTimeForm.getStartTime());
        receiveTimeConfig.setEndTime(storeTimeForm.getEndTime());
        receiveTimeConfigMapper.update(receiveTimeConfig,wrapper);
    }

    @Override
    public List<StoreInfo> findStoreByProductsId(Long productsId) {
        List<FoodInfo> list = foodInfoService.findFoodByProductsId(productsId);
        List<StoreInfo> stores =new ArrayList<>();
        for (FoodInfo foodInfo : list) {
            Long storeId = foodInfo.getStoreId();
            StoreInfo storeInfo = storeInfoMapper.selectById(storeId);
            stores.add(storeInfo);
        }
        return stores;
    }

    @Override
    public void addStoreInfo(AddStoreInfoForm addStoreInfoForm, String userId) {
        StoreInfo storeInfo = storeInfoMapper.selectById(userId);
        storeInfo.setLogo(addStoreInfoForm.getLogo());
        storeInfo.setOrderPhone(addStoreInfoForm.getOrderPhone());
        storeInfo.setInitiatePrice(addStoreInfoForm.getInitiatePrice());
        storeInfo.setDphPrice(addStoreInfoForm.getDphPrice());
        storeInfo.setLicense(addStoreInfoForm.getLicense());
        storeInfo.setPermit(addStoreInfoForm.getPermit());
        storeInfoMapper.updateById(storeInfo);

    }


}
